<template>
	<view>
		<view class="weui-switch" :class="{'weui-switch-on' : isChecked}" :value="value" @tap="toggle"></view>
	</view>
</template>

<script>
	export default {
		name: "com-switch",
		props: {
			value: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {
				isChecked: this.value
			}
		},
		watch: {
			value(val) {
				this.isChecked = val
			},
			isChecked(val) {
				this.$emit('change', val);
			}
		},
		methods: {
			toggle() {
				this.isChecked = !this.isChecked;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.weui-switch {
		display: block;
		position: relative;
		width: 104rpx;
		height: 52rpx;
		outline: 0;
		overflow: hidden;
		border-radius: 80rpx;
		box-sizing: border-box;
		background-color: #DFDFDF;
		transition: background-color 0.1s, border 0.1s;
		cursor: pointer;
	}

	.weui-switch:before {
		content: " ";
		position: absolute;
		top: 0;
		left: 0;
		width: 104rpx;
		height: 52rpx;
		border-radius: 80rpx;
		background-color: #bcbcbc;
		transition: transform 0.35s cubic-bezier(0.64, 1, 0.4, 1);
	}

	.weui-switch:after {
		content: " ";
		position: absolute;
		top: 8rpx;
		left: 8rpx;
		width: 36rpx;
		height: 36rpx;
		border-radius: 50%;
		background-color: #FFFFFF;
		box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
		transition: transform 0.35s cubic-bezier(0.4, 0.4, 0.25, 1.35);
	}

	.weui-switch-on {
		border-color: #6F6F6F;
		background-color: var(--primary-color);
	}

	.weui-switch-on:before {
		border-color: #1AAD19;
		background-color: var(--primary-color);
	}

	.weui-switch-on:after {
		transform: translateX(54rpx);
	}
</style>